package com.sinosoft.cnmuseum.utils;

import java.util.Collection;
import java.util.Map;
import java.util.UUID;

public class StringUtils extends wl.util.StringUtils {
	/**      
	 * isEmpty(是否为空判断) 
	 * @author wangli
	 * @param str
	 * @return          
	*/
	public static boolean isEmpty(String str){
		if(str == null || str.length() <= 0){
			return true;
		}
		return false;
	}
	
	/**      
	 * isNoEmpty(是否不为空判断) 
	 * @author wangli
	 * @param str
	 * @return          
	*/
	public static boolean isNoEmpty(String str){
		if(str == null || str.length() <= 0){
			return false;
		}
		return true;
	}
	
	
	/**      
	 * replace(替换字符串) 
	 * @author wangli
	 * @param src 源字符串
	 * @param oldstr 要替换的字符串
	 * @param newstr 新字符串
	 * @return          
	*/
	public static String replace(String src, String oldstr, String newstr) {
		StringBuffer dest = new StringBuffer();
		int beginIndex = 0;
		int endIndex = 0;
		while (true) {
			endIndex = src.indexOf(oldstr, beginIndex);
			if (endIndex >= 0) {
				dest.append(src.substring(beginIndex, endIndex));
				dest.append(newstr);
				beginIndex = endIndex + oldstr.length();
			} else {
				dest.append(src.substring(beginIndex));
				break;
			}
		}
		return dest.toString();
	}
	public static boolean isNullOrEmpty(Object obj) {
		try {
			if (obj == null)
				return true;
			if (obj instanceof CharSequence) {
				return ((CharSequence) obj).length() == 0;
			}
			if (obj instanceof Collection) {
				return ((Collection<?>) obj).isEmpty();
			}
			if (obj instanceof Map) {
				return ((Map<?, ?>) obj).isEmpty();
			}
			if (obj instanceof Object[]) {
				Object[] object = (Object[]) obj;
				if (object.length == 0) {
					return true;
				}
				boolean empty = true;
				for (int i = 0; i < object.length; i++) {
					if (!isNullOrEmpty(object[i])) {
						empty = false;
						break;
					}
				}
				return empty;
			}
			return false;
		} catch (Exception e) {
			return true;
		}

	}

	
	/**      
	 * sort 顺排序
	 * @author wangli
	 * @param ss
	 * @return          
	*/
	public static int[] sort(int[] ss){
		if(ss!=null&&ss.length>0){
			for(int i=1;i<ss.length;i++){
				int key = ss[i];
				int j = i-1;
				while(j>-1&&ss[j]>key){
					ss[j+1] = ss[j];
					j -= 1;
				}
				ss[j+1] = key;
			}
		}
		return ss;
	}
	
	/**      
	 * sortdesc 逆排序
	 * @author wangli
	 * @param ss
	 * @return          
	*/
	public static int[] sortdesc(int[] ss){
		if(ss!=null&&ss.length>0){
			for(int i=0;i<ss.length;i++){
				int key = ss[i];
				int j = i-1;
				while(j>-1&&ss[j]<key){
					ss[j+1] = ss[j];
					j -= 1;
				}
				ss[j+1] = key;
			}
		}
		return ss;
	}
	
	/**
	 * 生成字符串
	 * @return
	 */
	public static String getUUID24() {
		UUID id = UUID.randomUUID();
		String[] idd = id.toString().split("-");
		return idd[0] + idd[1] + idd[4];
	}
	
	
	public static void main(String[] args) {
		String str = "";
		String str2 = null;
		System.out.println(isNullOrEmpty(str));
		System.out.println(isNullOrEmpty(str2));
		System.out.println(isNullOrEmpty(0));
		
		
	}
	
}
